package com.school.gift.controller.admin;

import com.school.gift.domain.Business;
import com.school.gift.domain.Customer;
import com.school.gift.domain.User;
import com.school.gift.service.BusinessService;
import com.school.gift.service.CustomerService;
import com.school.support.base.AdminBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.exception.TipException;
import zhibi.common.support.response.Response;
import zhibi.common.util.security.SecurityUtil;

/**
 * Created by 执笔 on 2017/3/16.
 */
@RequestMapping("admin")
@Controller
public class AdminController extends AdminBaseController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private CustomerService customerService;

    @RequestMapping
    public String index() {
        return "admin/index";
    }


    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login() {
        return "admin/login";
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(String username, String password) {
        Business business = new Business();
        business.setUsername(username);
        business.setPassword(SecurityUtil.MD5(password, PASSWORD_COUNT));
        business = businessService.selectOne(business);
        if (null == business) {
            throw new TipException("用户名或者密码错误", "/admin/login");
        }
        session.setAttribute(SESSION_USER, business);
        return redirect("/admin/");
    }

    @RequestMapping("logout")
    public String logout() {
        session.removeAttribute(SESSION_USER);
        session.removeAttribute(SESSION_TYPE);
        return "admin/login";
    }

    @RequestMapping("modifyPwd")
    @ResponseBody
    public Response<String> modifyPwd(String password, String pwd, String pwd2) {
        if (!pwd.equals(pwd2)) {
            throw new MessageException("两次密码不一样");
        }
        Object sessionUser = session.getAttribute(SESSION_USER);
        if (null != sessionUser) {
            if (sessionUser instanceof Business) {
                Business business = (Business) sessionUser;
                business = businessService.selectByPK(business.getId());
                if (!business.getPassword().equalsIgnoreCase(SecurityUtil.MD5(password, PASSWORD_COUNT))) {
                    throw new MessageException("原密码不对");
                }
                business.setPassword(SecurityUtil.MD5(pwd, PASSWORD_COUNT));
                businessService.updateByIdSelective(business);
                session.removeAttribute(SESSION_USER);
                return Response.ok("business");
            } else {
                Customer customer = (Customer) sessionUser;
                customer = customerService.selectByPK(customer.getId());
                if (!customer.getPassword().equalsIgnoreCase(SecurityUtil.MD5(password, PASSWORD_COUNT))) {
                    throw new MessageException("原密码不对");
                }
                customer.setPassword(SecurityUtil.MD5(pwd, PASSWORD_COUNT));
                customerService.updateByIdSelective(customer);
                session.removeAttribute(SESSION_USER);
                return Response.ok("customer");
            }
        }
        return Response.faild("请先登录");
    }
}
